﻿Imports System.Text
Imports System.Threading
Imports System.Web
Imports System.Runtime.InteropServices
Imports Sy.UI
Imports Sy.UI.Cef
Imports Sy.Data
Imports Sy.UI.Common

Public Class Form1
    Dim dpurl As String
    Dim allasin As String
    Dim allasins As New Collection
    Dim allsearchs As New Collection
    Dim allbings As New Collection
    Dim lockobject As New Object
    Dim lockobjected As New Object
    Dim lockobjectebing As New Object
    Dim uploadxc As Thread
    Dim yanzheng As Thread
    Dim bingseach As Thread
    Dim getserver As Thread
    Dim bingisworking As Boolean = False
    Dim getserverworking As Boolean = False
    Dim myyz As Boolean = False
    Dim isworking As Boolean = False
    Dim yanzhengisworking As Boolean = False

    <DllImport("urlmon.dll", CharSet:=CharSet.Ansi)>
    Private Shared Function UrlMkSetSessionOption(ByVal dwOption As Integer, ByVal pBuffer As String, ByVal dwBufferLength As Integer, ByVal dwReserved As Integer) As Integer
    End Function
    Const URLMON_OPTION_USERAGENT As Integer = &H10000001
    Private Shared defaultUserAgent As String = "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.164 Safari/537.36"
    Public Shared Sub ChangeUserAgent(ByVal userAgent As String)
        UrlMkSetSessionOption(URLMON_OPTION_USERAGENT, userAgent, userAgent.Length, 0)
    End Sub
    Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        Try
            'DB = New Sy.Data.SQLite("create table [tasklist]([id] INTEGER PRIMARY KEY,[asin] TEXT,[dpurl] TEXT,[nodebrowser] TEXT,[nodename] TEXT,[review] TEXT,[stars] TEXT,[price] TEXT,[shipfee] TEXT,[totalprice] TEXT,[currency] TEXT,[bsr] TEXT,[brand] TEXT,[isreg] TEXT,[ischina] TEXT,[totalmark] TEXT,[status] TEXT,[c_time] TEXT,[page] TEXT,[caurl] TEXT,[keyword] TEXT);", Application.StartupPath & "\Config\task.db")
            Dim Create = CreateSQLBuilder(Of TaskList).CreateSQL()
            DB = New SQLite(Create, Application.StartupPath & "\Config\task.db")
            TaskList.AddDB(DB)
            DB.Foreign_keys = True
            Dim Create2 = CreateSQLBuilder(Of TaskList7).CreateSQL()
            DB2 = New SQLite(Create2, Application.StartupPath & "\Config\task7.db")
            TaskList7.AddDB(DB2)
            'DB = New Sy.Data.SQLite("create table [calist]([id] INTEGER PRIMARY KEY,[asin] TEXT,[dpurl] TEXT);", Application.StartupPath & "\task2.db")
            country.Items.Add("Amazon.com")
            country.Items.Add("Amazon.ca")
            country.Items.Add("Amazon.com.mx")
            country.Items.Add("Amazon.co.uk")
            country.Items.Add("Amazon.fr")
            country.Items.Add("Amazon.de")
            country.Items.Add("Amazon.it")
            country.Items.Add("Amazon.es")
            country.Items.Add("Amazon.se")
            country.Items.Add("Amazon.nl")
            country.Items.Add("Amazon.pl")
            country.Items.Add("Amazon.com.au")
            country.Items.Add("Amazon.co.jp")
            country.Items.Add("Amazon.in")
            country.Items.Add("Amazon.ae")
            country.SelectedIndex = 0
            cy.Items.Add("")
            cy.Items.Add("Amazon.com")
            cy.Items.Add("Amazon.ca")
            cy.Items.Add("Amazon.com.mx")
            cy.Items.Add("Amazon.co.uk")
            cy.Items.Add("Amazon.fr")
            cy.Items.Add("Amazon.de")
            cy.Items.Add("Amazon.it")
            cy.Items.Add("Amazon.es")
            cy.Items.Add("Amazon.se")
            cy.Items.Add("Amazon.nl")
            cy.Items.Add("Amazon.pl")
            cy.Items.Add("Amazon.com.au")
            cy.Items.Add("Amazon.co.jp")
            cy.Items.Add("Amazon.in")
            cy.Items.Add("Amazon.ae")
            cy.SelectedIndex = 0
            sb.Items.Add("")
            sb.Items.Add("未知")
            sb.Items.Add("未注册")
            sb.Items.Add("已注册")
            sb.SelectedIndex = 0
            js.Items.Add("")
            js.Items.Add("未知")
            js.Items.Add("是")
            js.Items.Add("否")
            js.SelectedIndex = 0
            is_china.Items.Add("")
            is_china.Items.Add("未知")
            is_china.Items.Add("是")
            is_china.Items.Add("否")
            is_china.SelectedIndex = 0
            tabsindex = TabPage1
            CefWeb = CefBrowser.LoadBrowser(Pnl_Web, "about:_blank")
            CefWeb2 = CefBrowser.LoadBrowser(Pnl_Web2, "about:_blank")
            CefWeb3 = CefBrowser.LoadBrowser(Pnl_Web3, "about:_blank")
            UIAdapter.ListviewAdapter(Of TaskList)(ListView2, "序号")
            Loadlist("")

            'AddHandler CefWeb.SourceCompleted, Sub(cef As CefBrowser, Url As String)
            '                                       MsgBox(Url.Length)
            '                                   End Sub
            'Form5.Show()

            Me.AcceptButton = Me.Btstart
        Catch ex As Exception
            MsgBox("程序载入异常，请重试！", MsgBoxStyle.OkOnly, "提示")
        End Try
    End Sub
    'Private Sub CefWeb_DocumentCompleted(sender As CefBrowser, DocumentCompletedEventArgs As DocumentCompletedEventArgs) Handles CefWeb.DocumentCompleted

    'End Sub
    Sub loadtaskname()
        Dim sql = "select distinct Taskname  from t_TaskList;"
        Dim reader As Data.Common.DbDataReader = DB(sql)
        Do While reader.Read
            taskname.Items.Add(reader("Taskname").ToString)
        Loop
        reader.Close()
    End Sub
    Private Sub TabControl1_Selected(sender As Object, e As TabControlEventArgs) Handles TabControl1.Selected
        'If e.TabPage.TabIndex = TabPage2.TabIndex Then
        '    If isworking = True Then
        '        MsgBox("请先停止任务再筛选")
        '        TabControl1.SelectedTab = tabsindex
        '        Exit Sub
        '    Else
        '        tabsindex = TabPage2
        '        Me.AcceptButton = Me.Button8
        '        Loadlist("")
        '    End If
        'End If
        'If e.TabPage.TabIndex = TabPage3.TabIndex Then
        '    If isworking = True Then
        '        MsgBox("请先停止任务再筛选")
        '        TabControl1.SelectedTab = tabsindex
        '        Exit Sub
        '    Else
        '        tabsindex = TabPage3
        '        Me.AcceptButton = Me.Button8
        '        Loadlist("")
        '    End If
        'End If

        If isworking = True Or bingisworking = True Or yanzhengisworking = True Then
            MsgBox("请先停止任务再筛选")
            TabControl1.SelectedTab = tabsindex
            Exit Sub
        Else
            If e.TabPage.TabIndex = TabPage3.TabIndex Then
                tabsindex = TabPage3
            End If
            If e.TabPage.TabIndex = TabPage1.TabIndex Then
                tabsindex = TabPage1
            End If
            If e.TabPage.TabIndex = TabPage2.TabIndex Then
                tabsindex = TabPage2
                Me.AcceptButton = Me.Button8
                Loadlist("")
                loadtaskname()
            End If
        End If
    End Sub

    Private Sub TextBox1_TextChanged(sender As Object, e As EventArgs) Handles shuchu.TextChanged
        Try
            If UBound(Split(shuchu.Text, vbCrLf)) > 9 Then
                shuchu.Text = Mid(shuchu.Text, InStr(shuchu.Text, vbCrLf) + 2)
            End If
            shuchu.SelectionStart = Len(shuchu.Text)
            shuchu.ScrollToCaret()
        Catch ex As Exception
            MsgBox("状态输出异常！", MsgBoxStyle.OkOnly, "提示")
        End Try
    End Sub

    Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
        Try
            For i = 0 To ListView2.Items.Count - 1
                If ListView2.Items(i).Checked = True Then
                    'DB.Execute("delete from tasklist where ID='" & ListView2.Items(i).SubItems(1).Text & "'")
                    Dim TaskList As TaskList = ListView2.Items(i).Tag
                    TaskList.Del()
                End If
            Next
            Call Loadlist("")
        Catch ex As Exception
            MsgBox("删除出错", MsgBoxStyle.OkOnly, "提示")
        End Try
    End Sub

    Private Sub Timer1_Tick(sender As Object, e As EventArgs) Handles Timer1.Tick
        Try
            Dim url = "https://www.miwaimao.com/softlogin.php?UserName=" & UserNamelo & "&Version=" & Version & "&step=2&logintime=" & logintime
            Console.WriteLine(url)
            'shuchu.Text &= url.ToString & vbCrLf
            Dim html = getnewhtml(url)
            'shuchu.Text &= html.ToString & vbCrLf
            If html = "8" Then
                MsgBox("您的账户已在别处登录，当前会话退出！")
                Timer1.Enabled = False
                End
            End If
        Catch ex As Exception
            MsgBox("登录异常，当前会话退出！")
            Timer1.Enabled = False
            End
        End Try
    End Sub
    Sub Loadlist(sqlstr As String)
        Try
            With ListView2
                .Items.Clear()
            End With
            Dim TaskLists = If(sqlstr.IsNotWhiteSpace, TaskList.LoadWhere(sqlstr), TaskList.Load())
            Dim i As Long = 1
            For Each TaskList In TaskLists
                Dim a = UIAdapter.GetListViewItem(TaskList, i)
                Dim itm As ListViewItem = ListView2.Items.Add(a) '状态 进度 id
                itm.Tag = TaskList
                'itm = ListView2.Items.Add(i)
                'itm.SubItems.AddRange({TaskList.ObjectDBID, TaskList.Asin, TaskList.Dpurl, CStr(reader.Item("nodebrowser")), CStr(reader.Item("nodename")), CStr(reader.Item("review")), CStr(reader.Item("price")), CStr(reader.Item("shipfee")), CStr(reader.Item("totalprice")), CStr(reader.Item("currency")), CStr(reader.Item("bsr")), CStr(reader.Item("brand")), CStr(reader.Item("isreg")), CStr(reader.Item("ischina")), CStr(reader.Item("totalmark")), CStr(reader.Item("c_time"))})
                i = i + 1
            Next
        Catch ex As Exception
            MsgBox("加载数据异常", MsgBoxStyle.OkOnly, "提示")
        End Try
    End Sub

    Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
        Loadlist("")
    End Sub

    Private Sub CheckBox1_CheckedChanged(sender As Object, e As EventArgs) Handles CheckBox1.CheckedChanged
        If CheckBox1.Checked = True Then
            For I = 0 To ListView2.Items.Count - 1
                ListView2.Items(I).Checked = True
            Next
        Else
            For I = 0 To ListView2.Items.Count - 1
                ListView2.Items(I).Checked = False
            Next
        End If
    End Sub

    Private Sub Button3_Click(sender As Object, e As EventArgs) Handles Button3.Click
        Try
            Dim TaskList As TaskList
            TaskList.Execute("delete from t_TaskList where 1=1")
            Loadlist("")
        Catch ex As Exception
            MsgBox("删除出错", MsgBoxStyle.OkOnly, "提示")
        End Try
    End Sub

    Private Sub Btend_Click(sender As Object, e As EventArgs) Handles Btend.Click
        Try
            'Dim a = "Liczba ocen: 173"
            'Console.WriteLine(GetNumbers(a))
            isworking = False
            bingisworking = False
            yanzhengisworking = False
            getserverworking = False
            If uploadxc.IsAlive Then
                uploadxc.Abort()
            End If
            If yanzheng.IsAlive Then
                yanzheng.Abort()
            End If
            If bingseach.IsAlive Then
                bingseach.Abort()
            End If
            If getserver.IsAlive Then
                getserver.Abort()
            End If

            GC.Collect()
            GC.WaitForFullGCApproach()
            Btend.Enabled = False
            Btstart.Enabled = True
            MsgBox("已经停止", MsgBoxStyle.OkOnly, "提示")
        Catch ex As Exception
            Btend.Enabled = False
            Btstart.Enabled = True
            isworking = False
            bingisworking = False
            yanzhengisworking = False
            getserverworking = False
            MsgBox("已经停止", MsgBoxStyle.OkOnly, "提示")
        End Try
    End Sub


    Private Sub Button4_Click(sender As Object, e As EventArgs) Handles Button4.Click
        Try
            Dim aas As String = ""
            Dim str As String = "Asin,网址,产品标题,节点ID,节点名称,评论数,星级,品牌名,是否注册,是否自发货,是否僵尸产品,BSR,上架时间" & vbCrLf
            Dim sql As String = "select * from t_TaskList;"
            Dim reader As Data.Common.DbDataReader = DB(sql)
            Do While reader.Read
                If InStr(aas, reader("Asin").ToString) < 1 Then
                    aas &= reader("Asin").ToString
                    str &= reader("Asin").ToString & "," & reader("Dpurl").ToString & ",""" & reader("Title").ToString & """," & reader("Nodebrowser").ToString & ",""" & reader("Nodename").ToString & """," & reader("Review").ToString & "," & reader("Stars").ToString & "," & reader("Brand").ToString & "," & reader("IsReg").ToString & "," & reader("IsChina").ToString & "," & reader("Status").ToString & "," & reader("BSR").ToString & ",""" & reader("C_Time").ToString & """" & vbCrLf
                End If
            Loop
            reader.Close()
            Dim filename = Format(Now(), "yyyy-MM-dd") & ".csv"
            My.Computer.FileSystem.WriteAllText(Application.StartupPath & "\" & filename, str, True, Encoding.Default)
            shuchu.Text &= "导出文件成功：" & Application.StartupPath & "\" & filename
            MsgBox("导出成功，请在程序根目录下查看文件！", MsgBoxStyle.OkOnly, "提示")
        Catch ex As Exception
            MsgBox("导出出错！", MsgBoxStyle.OkOnly, "提示")
        End Try
    End Sub

    Private Sub Btstart_Click(sender As Object, e As EventArgs) Handles Btstart.Click
        Try
            If Trim(keyword.Text) = "" Then
                MsgBox("关键词不能为空！", MsgBoxStyle.OkOnly, "提示")
                Exit Sub
            End If
            If tradecookie = "" And isyz.Checked Then
                MsgBox("验证开启时请先获取Cookie！", MsgBoxStyle.OkOnly, "提示")
                Exit Sub
            End If
            searchkeyword = keyword.Text
            dpurl = ""
            myyz = isyz.Checked
            allsearchs.Clear()
            allbings.Clear()
            'If keyword.Items.Count <= 0 Then
            '    keyword.Items.Add(keyword.Text)
            'End If
            For Each item In keyword.Items
                If country.Text = "Amazon.com" Or country.Text = "Amazon.com.au" Or country.Text = "Amazon.ca" Or country.Text = "Amazon.co.uk" Or country.Text = "Amazon.ae" Then
                    dpurl = HttpUtility.UrlEncode("site:" & country.Text & " /dp/ ""Currently+unavailable"" " & item.ToString)
                End If
                If country.Text = "Amazon.com.mx" Then
                    dpurl = HttpUtility.UrlEncode("site:" & country.Text & " /dp/ ""No disponible por el momento"" " & item.ToString)
                End If
                If country.Text = "Amazon.fr" Then
                    dpurl = HttpUtility.UrlEncode("site:" & country.Text & " /dp/ ""Actuellement+indisponible"" " & item.ToString)
                End If
                If country.Text = "Amazon.de" Then
                    dpurl = HttpUtility.UrlEncode("site:" & country.Text & " /dp/ ""Derzeit nicht verfügbar"" " & item.ToString)
                End If
                If country.Text = "Amazon.it" Then
                    dpurl = HttpUtility.UrlEncode("site:" & country.Text & " /dp/ ""Non+disponibile"" " & item.ToString)
                End If
                If country.Text = "Amazon.es" Then
                    dpurl = HttpUtility.UrlEncode("site:" & country.Text & " /dp/ ""Agotado+temporalmente"" " & item.ToString)
                End If
                If country.Text = "Amazon.se" Then
                    dpurl = HttpUtility.UrlEncode("site:" & country.Text & " /dp/ ""För närvarande inte tillgänglig"" " & item.ToString)
                End If
                If country.Text = "Amazon.nl" Then
                    dpurl = HttpUtility.UrlEncode("site:" & country.Text & " /dp/ ""Momenteel niet verkrijgbaar"" " & item.ToString)
                End If
                If country.Text = "Amazon.pl" Then
                    dpurl = HttpUtility.UrlEncode("site:" & country.Text & " /dp/ ""Obecnie+niedostępny"" " & item.ToString)
                End If
                If country.Text = "Amazon.co.jp" Then
                    dpurl = HttpUtility.UrlEncode("site:" & country.Text & " /dp/ 現在在庫切れ " & item.ToString)
                End If
                If country.Text = "Amazon.in" Then
                    dpurl = HttpUtility.UrlEncode("site:" & country.Text & " /dp/ ""इस समय उपलब्ध नहीं है"" " & item.ToString)
                End If
                allsearchs.Add(dpurl)
                allbings.Add(dpurl)
            Next
            isworking = True
            bingisworking = True
            yanzhengisworking = True
            getserverworking = True
            Thecountry = country.Text
            allasin = ""
            allasins.Clear()
            Dim sqlstr2 = "select * from t_TaskList order by ObjectDBID desc;"
            Dim reader2 As Data.Common.DbDataReader = DB(sqlstr2)
            If reader2.HasRows Then
                With reader2.Read
                    allasin &= CStr(reader2.Item("Dpurl"))
                End With
            End If
            shuchu.Text = ""
            ListView1.Items.Clear()
            Btstart.Enabled = False
            uploadxc = New System.Threading.Thread(New System.Threading.ParameterizedThreadStart(AddressOf caiji))
            uploadxc.Name = NameOf(caiji)
            uploadxc.Start()   '启动线程
            bingseach = New System.Threading.Thread(New System.Threading.ParameterizedThreadStart(AddressOf caijibing))
            bingseach.Name = NameOf(caijibing)
            bingseach.Start()   '启动线程
            yanzheng = New System.Threading.Thread(New System.Threading.ParameterizedThreadStart(AddressOf yz))
            yanzheng.Name = NameOf(yz)
            yanzheng.Start()   '启动线程 
            'getserver = New System.Threading.Thread(New System.Threading.ParameterizedThreadStart(AddressOf getcaiji))
            'getserver.Name = NameOf(getcaiji)
            'getserver.Start()   '启动线程
            Btend.Enabled = True
        Catch ex As Exception
            MsgBox("获取中断，已经停止！", MsgBoxStyle.OkOnly, "提示")
            Btend.Enabled = False
            Btstart.Enabled = True
            isworking = False
            bingisworking = False
            yanzhengisworking = False
            getserverworking = False
        End Try
    End Sub
    Sub getcaiji()
        Dim asid As String = "0"
        Do
            Try
                Dim ull = "https://www.miwaimao.com/api/jiangshi.php?action=select&keyword=" & searchkeyword & "&id=" & asid
                Dim htmls = Sy.Net.Get(ull)
                If htmls = "null" Then
                    getserverworking = False
                    Exit Sub
                End If
                Console.WriteLine(ull)
                Dim asg = Split(htmls, "|||")
                For i = 0 To UBound(asg) - 1
                    Dim shal = Split(asg(i), "=")
                    asid = shal(0)
                    Dim aa = shal(1)
                    If InStr(allasin, aa) <= 0 Then
                        allasin &= aa
                        Console.WriteLine(aa)
                        allasins.Add(aa)
                    End If
                    Application.DoEvents()
                Next
            Catch ex As Exception
                getserverworking = False
                Exit Sub
            End Try
            Thread.Sleep(2000)
            Application.DoEvents()
        Loop
    End Sub
    Function getasinfromCollection() As String
        Try
            SyncLock lockobject
                If allasins.Count > 0 Then
                    Dim aa = allasins(1)
                    Console.WriteLine(aa)
                    allasins.Remove(1)
                    Return aa.ToString
                Else
                    Return ""
                End If
            End SyncLock
        Catch ex As Exception
            Return ""
        End Try
    End Function
    Function getsearchfromCollection() As String
        Try
            SyncLock lockobjected
                If allsearchs.Count > 0 Then
                    Dim aa = allsearchs(1)
                    Console.WriteLine(aa)
                    allsearchs.Remove(1)
                    Return aa.ToString
                Else
                    Return ""
                End If
            End SyncLock
        Catch ex As Exception
            Return ""
        End Try
    End Function
    Function getbingfromCollection() As String
        Try
            SyncLock lockobjectebing
                If allbings.Count > 0 Then
                    Dim aa = allbings(1)
                    Console.WriteLine(aa)
                    allbings.Remove(1)
                    Return aa.ToString
                Else
                    Return ""
                End If
            End SyncLock
        Catch ex As Exception
            Return ""
        End Try
    End Function

    Sub yz()
        Do
            Try
                If isworking = False And bingisworking = False And allasins.Count < 1 Then
                    Invoke(Sub()
                               yanzhengisworking = False
                               shuchu.Text &= "已经停止" & vbCrLf
                               Btend.Enabled = False
                               Btstart.Enabled = True
                           End Sub)
                    Exit Sub
                End If
                Dim review As String = 0
                Dim brand As String = ""
                Dim status As String = "未知"
                Dim isreg As String = "未知"
                Dim isfbm As String = "未知"
                Dim stars As String = 0
                Dim urlc As String = ""
                Dim title As String = ""
                Dim nodebrowser As String = ""
                Dim nodename As String = ""
                Dim bsr As String = 0
                Dim c_time As String = "未知"
                Dim Asin = getasinfromCollection()
                If Asin <> "" Then
                    If myyz = True Then
                        'Dim html = getnewhtml(urlc, "GET", "", amazoncookie)
                        urlc = "https://www." & Thecountry & "/dp/" & Asin & "?&psc=1"
                        CefWeb2.Load(urlc)
                        Application.DoEvents()
                        Thread.Sleep(2000)
                        Dim html = waitfor(CefWeb2, urlc, "meow", "validatecaptcha", Me, "error/logo._ttd_.png", "ref=cs_404_logo")
                        'My.Computer.FileSystem.WriteAllText(Application.StartupPath & "\2.txt", html, True, Encoding.Default)
                        If InStr(html.ToLower, "add-to-cart-button") <= 0 And InStr(html.ToLower, "gestalt-buybox") <= 0 And InStr(html.ToLower, "gestalt-popover-button-announce") <= 0 And InStr(html.ToLower, "error/logo._ttd_.png") <= 0 And InStr(html.ToLower, "ref=cs_404_logo") <= 0 Then
                            status = "是"
                        Else
                            status = "否"
                        End If
                        review = GetreviewNumbers(getreview(html))
                        brand = getbrand(html)
                        stars = GetNumbers(getstars(html))
                        title = gettitle(html)
                        isfbm = getfba(html)
                        bsr = getbsr(html)
                        nodebrowser = getnodebrowser(html)
                        nodename = getnodenodename(html)
                        c_time = getc_time(html)
                        'Console.WriteLine(brand & ":" & review & ":" & stars)
                        If brand <> "" And brand.Length < 100 Then
                            If brandlistname = "tmdn.org" Then
                                Dim htmls = getnewhtml("https://www.tmdn.org/tmview/api/search/autocomplete/basicSearch?text=" & HttpUtility.UrlEncode(brand), "GET", "", tradecookie)
                                If InStr(htmls, "here are your search results") > 0 Then
                                    isreg = "已注册"
                                Else
                                    isreg = "未注册"
                                End If
                            End If
                            If brandlistname = "ip51.com" Then
                                Dim formdata = "{""keyWords"":[""" & brand & """],""sources"":[],""ncl"":[],""Keyword"":""" & brand & """,""status"":""0""}"
                                Dim htmls = getnewhtml("https://www.ip51.com/api/brand/search", "POST", formdata, "", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.164 Safari/537.36", "https://www.ip51.com/brand/search", "*/*", "application/json")
                                If InStr(htmls, "{""brands"":[]}") > 0 Then
                                    isreg = "未注册"
                                Else
                                    isreg = "已注册"
                                End If
                            End If
                            If brandlistname = "trademarkia.com" Then
                                Dim nurl = "https://www.trademarkia.com/trademarks-search.aspx?tn=" & HttpUtility.UrlEncode(brand)
                                CefWeb2.Load(nurl)
                                'Thread.Sleep(1000)
                                'Console.WriteLine(nurl)
                                Dim page = waitfor(CefWeb2, nurl, "txt txt-lg")
                                If InStr(page.ToLower, "here are your search results") > 0 Then
                                    isreg = "已注册"
                                Else
                                    isreg = "未注册"
                                End If
                            End If
                        End If
                    End If
                    If (title <> "" And myyz = True) Or myyz = False Then
                        Dim t As New TaskList
                        t.Asin = Asin
                        t.Brand = brand
                        t.BSR = bsr
                        t.Caurl = ""
                        t.Currency = ""
                        t.C_Time = c_time
                        t.IsReg = isreg
                        t.Dpurl = urlc
                        t.IsChina = isfbm
                        t.Page = ""
                        t.Price = 0
                        t.Review = review
                        t.Stars = stars
                        t.Status = status
                        t.Totalmark = 0
                        t.Totalprice = 0
                        t.Title = title
                        t.Shipfee = 0
                        t.Nodebrowser = nodebrowser
                        t.Nodename = nodename
                        t.Country = Thecountry
                        t.Taskname = searchkeyword
                        t.Add()
                        'Dim tjurl As String = ""
                        'If status = "是" And Asin <> "" Then
                        '    tjurl = "https://www.miwaimao.com/api/jiangshi.php?action=add&Asin=" & Asin.ToString & "&Brand=" & brand.ToString & "&BSR=" & bsr.ToString & "&Nodebrowser=" & nodebrowser.ToString & "&Nodename=" & nodename.ToString & "&IsReg=" & isreg.ToString & "&Dpurl=" & urlc & "&IsChina=" & isfbm.ToString & "&Review=" & review & "&Stars=" & stars & "&Totalmark=" & "&Title=" & title & "&Country=" & Thecountry & "&User=" & UserNamelo & "&keyword=" & searchkeyword
                        'Else
                        '    tjurl = "https://www.miwaimao.com/api/jiangshi.php?action=del&Asin=" & Asin.ToString & "&Brand=" & brand.ToString & "&BSR=" & bsr.ToString & "&Nodebrowser=" & nodebrowser.ToString & "&Nodename=" & nodename.ToString & "&IsReg=" & isreg.ToString & "&Dpurl=" & urlc & "&IsChina=" & isfbm.ToString & "&Review=" & review & "&Stars=" & stars & "&Totalmark=" & "&Title=" & title & "&Country=" & Thecountry & "&User=" & UserNamelo & "&keyword=" & searchkeyword
                        'End If

                        'Dim htmls = getnewhtml(tjurl)
                        Invoke(Sub()
                                   Dim itm As ListViewItem
                                   itm = ListView1.Items.Add(ListView1.Items.Count + 1)
                                   itm.SubItems.AddRange({"", CStr(Asin), CStr(trimsql(urlc)), CStr(title), CStr(nodebrowser), CStr(nodename), CStr(review), CStr(stars), CStr(brand), CStr(isreg), CStr(status), CStr(isfbm), CStr(bsr), CStr(c_time), CStr(Thecountry)})
                                   shuchu.Text &= urlc & vbCrLf
                               End Sub)
                    End If
                End If
            Catch ex As Exception
                Invoke(Sub()
                           yanzhengisworking = False
                           isworking = False
                           bingisworking = False
                           getserverworking = False
                           shuchu.Text &= "已经停止" & vbCrLf
                           Btend.Enabled = False
                           Btstart.Enabled = True
                       End Sub)
                Exit Sub
            End Try
            Thread.Sleep(2000)
            Application.DoEvents()
        Loop
    End Sub
    Sub caijibing()
        Try
            Do
                Dim i As Integer = 0
                Dim pagehtml = ""
                Dim ndpurl = ""
                If allbings.Count <= 0 Then
                    bingisworking = False
                    Exit Sub
                End If
                If i >= 100 Or i = 0 Then
                    ndpurl = getbingfromCollection()
                End If

                For i = 0 To 100
                    Dim URL = "https://www.bing.com/search?q=" & Replace(ndpurl, " ", "+")
                    If i > 0 Then
                        URL &= "&first=" & (i * 10 + 1)
                    End If
                    'If i >= 100 Then
                    '    bingisworking = False
                    '    Exit Sub
                    'End If
                    'Console.WriteLine(URL)
                    'URL = "https://www.bing.com/search?q=site%3aAmazon.com+%2fdp%2f+%22Currently+unavailable%22+dress"
                    'My.Computer.FileSystem.WriteAllText(Application.StartupPath & "\0.txt", URL, True, Encoding.Default)

                    'My.Computer.FileSystem.WriteAllText(Application.StartupPath & "\aaa.html", pagehtml, True, Encoding.Default)
                    'pagehtml = My.Computer.FileSystem.ReadAllText(Application.StartupPath & "\aaa.html", Encoding.Default)
                    'pagehtml = getnewhtml(URL, "GET", "", "", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/100.0.4896.127 Safari/537.36")
                    'My.Computer.FileSystem.WriteAllText(Application.StartupPath & "\aaa.html", pagehtml, True, Encoding.Default)
                    CefWeb3.Load(URL)
                    Application.DoEvents()
                    Thread.Sleep(2000)
                    Console.WriteLine(URL)
                    pagehtml = waitfor(CefWeb3, URL, "script></body></html>")
                    If InStr(pagehtml, "<h2><a target=""_blank"" href=""") > 0 Then
                        Dim urla = Split(pagehtml, "<h2><a target=""_blank"" href=""")
                        For r = 1 To UBound(urla)
                            Dim urlc As String = Split(urla(r), """ h=""ID=")(0)
                            If InStr(urlc.ToLower, "/dp/") > 0 And InStr(allasin, urlc) <= 0 And InStr(urlc.ToLower, "amazon") > 0 Then
                                allasin &= urlc
                                Dim asin = Split(urlc, "/dp/")(1)
                                Console.WriteLine(allasins.Count)
                                allasins.Add(asin)

                            End If
                            Application.DoEvents()
                        Next
                    Else
                        'bingisworking = False
                        i = 100
                        Exit For
                    End If
                    Thread.Sleep(1000)
                    Application.DoEvents()
                    'i += 1
                Next
            Loop
        Catch ex As Exception
            bingisworking = False
            Exit Sub
        End Try
    End Sub
    Sub caiji()

        Try
            Do
                Dim i As Integer = 0
                Dim pagehtml = ""
                Dim ndpuurl = ""

                If allsearchs.Count <= 0 Then
                    'Invoke(Sub()
                    '           shuchu.Text &= "已经停止" & vbCrLf
                    '           Btend.Enabled = False
                    '           Btstart.Enabled = True
                    '       End Sub)
                    isworking = False
                    Exit Sub
                End If
                Dim urla
                Dim plitstra = """ target=""_self"" rel=""noopener"
                Dim plitstrb = "js-result-title"" href="""
                Dim plitstrc = "data-header-feature=""0"""



                If i >= 100 Or i = 0 Then
                    ndpuurl = getsearchfromCollection()
                End If
                For i = 0 To 100
                    If InStr(ndpuurl, "co.jp") > 0 Then
                        Dim URL = "https://www.google.com/search?q=" & ndpuurl & "&start=" & (i * 10)
                        CefWeb.Load(URL)
                        Application.DoEvents()
                        Thread.Sleep(2000)
                        pagehtml = waitfor(CefWeb, URL, "id=""pnnext")
                        'pagehtml = My.Computer.FileSystem.ReadAllText(Application.StartupPath & "\1.txt", Encoding.Default)
                        'Console.WriteLine(URL)


                        If InStr(pagehtml, plitstrc) > 0 Then
                            urla = Split(pagehtml, plitstrc)
                        End If
                        'My.Computer.FileSystem.WriteAllText(Application.StartupPath & "\1.txt", pagehtml, True, Encoding.Default)
                    Else
                        Dim URL = "https://www.ecosia.org/search?q=" & ndpuurl & "&p=" & i
                        Console.WriteLine(URL)
                        'My.Computer.FileSystem.WriteAllText(Application.StartupPath & "\0.txt", URL, True, Encoding.Default)
                        CefWeb.Load(URL)
                        Application.DoEvents()
                        Thread.Sleep(2000)
                        'Dim pagehtml = waitfor(CefWeb, "main-footer__menu--social")
                        'pagehtml = waitfor(CefWeb, URL, "aria-label=""next", "</html>")
                        pagehtml = waitfor(CefWeb, URL, "</html>")
                        'My.Computer.FileSystem.WriteAllText(Application.StartupPath & "\aaa.html", pagehtml, True, Encoding.Default)
                        'pagehtml = My.Computer.FileSystem.ReadAllText(Application.StartupPath & "\aaa.html", Encoding.Default)


                        If InStr(pagehtml, plitstra) > 0 Then
                            urla = Split(pagehtml, plitstra)
                        End If
                        If InStr(pagehtml, plitstrb) > 0 Then
                            urla = Split(pagehtml, plitstrb)
                        End If
                    End If
                    'Console.WriteLine(urla.Count)

                    For r = 0 To UBound(urla) - 1
                        Dim urlc As String = ""
                        Dim urlb
                        Dim urld
                        If InStr(pagehtml, plitstra) > 0 And InStr(pagehtml, plitstrc) < 1 Then
                            urlb = Split(urla(r), "href=""")
                            urlc = urlb(UBound(urlb)).ToString
                        End If
                        If InStr(pagehtml, plitstrb) > 0 And InStr(pagehtml, plitstrc) < 1 Then
                            urlb = Split(urla(r), """ ")
                            urlc = urlb(0)
                        End If
                        If InStr(pagehtml, plitstrc) > 0 Then
                            urlb = Split(urla(r + 1), "href=""")(1)
                            urld = Split(urlb, """ data-ved=")
                            urlc = urld(0)
                        End If



                        If InStr(urlc.ToLower, "/dp/") > 0 And InStr(allasin, urlc) <= 0 And InStr(urlc.ToLower, "amazon") > 0 Then
                            'Console.WriteLine(urlc)
                            allasin &= urlc
                            Dim asin = Split(urlc, "/dp/")(1)
                            Console.WriteLine(asin)
                            Console.WriteLine(allasins.Count)
                            allasins.Add(asin)
                        End If
                        Application.DoEvents()
                    Next

                    'Console.WriteLine(pagehtml)
                    'If InStr(pagehtml, "pagination__current-text") > 0 Then

                    'Dim pagelist = Split(pagehtml, "pagination__current-text")(1)
                    If InStr(pagehtml.ToLower, "aria-label=""next") > 0 Then
                        Dim pagelist = Split(pagehtml.ToLower, "aria-label=""next")(0)
                        Dim pagelistb = Split(pagelist, "href=""")
                        If InStr(pagelistb(UBound(pagelistb)), "/search?") <= 0 Then
                            'Console.WriteLine(pagehtml)
                            i = 100
                            Exit For
                            'isworking = False
                        End If
                    ElseIf InStr(pagehtml, "id=""pnnext") <= 0 Then
                        'Console.WriteLine(pagehtml)
                        i = 100
                        Exit For
                        'isworking = False
                    End If
                    Thread.Sleep(1000)
                    Application.DoEvents()
                    'i += 1
                    Console.WriteLine(i & "===========")
                Next

            Loop
        Catch ex As Exception
            isworking = False
            Exit Sub
        End Try
    End Sub

    Private Sub isyz_CheckedChanged(sender As Object, e As EventArgs) Handles isyz.CheckedChanged
        Try
            If isyz.Checked Then
                Form4.Show()
            End If
        Catch ex As Exception

        End Try
    End Sub

    Private Sub Button5_Click(sender As Object, e As EventArgs) Handles Button5.Click
        Try
            'Dim sqlstr = "select * from t_TaskList where 1=1"
            'sqlstr &= " And Review>=" & pla.Value & " And Review<=" & plb.Value & " And Stars>=" & xja.Value & " And Stars<=" & xjb.Value & " And IsReg='" & sb.Text & "' and Status='" & js.Text & "'"
            'If keyw.Text <> "" Then
            '    sqlstr &= " and Keyword like '%" & trimsql(keyw.Text) & "%'"
            'End If
            'sqlstr &= " order by Keyword desc;"
            'Dim str As String = ""
            'Dim reader As Data.Common.DbDataReader = DB(sqlstr)
            'Do While reader.Read
            '    str &= reader("Asin").ToString & vbCrLf
            'Loop
            'reader.Close()
            Dim str As String = "Asin,网址,产品标题,节点ID,节点名称,评论数,星级,品牌名,是否注册,是否自发货,是否僵尸产品,BSR,上架时间" & vbCrLf
            Dim aas As String = ""
            For I = 0 To ListView2.Items.Count - 1
                If ListView2.Items(I).Checked = True Then
                    If InStr(aas, ListView2.Items(I).SubItems(1).Text) < 1 Then
                        aas &= ListView2.Items(I).SubItems(1).Text
                        str &= ListView2.Items(I).SubItems(1).Text & "," & ListView2.Items(I).SubItems(2).Text & ",""" & ListView2.Items(I).SubItems(3).Text & """,""" & ListView2.Items(I).SubItems(4).Text & """,""" & ListView2.Items(I).SubItems(5).Text & """," & ListView2.Items(I).SubItems(6).Text & "," & ListView2.Items(I).SubItems(7).Text & "," & ListView2.Items(I).SubItems(8).Text & "," & ListView2.Items(I).SubItems(9).Text & "," & ListView2.Items(I).SubItems(10).Text & "," & ListView2.Items(I).SubItems(11).Text & "," & ListView2.Items(I).SubItems(12).Text & ",""" & ListView2.Items(I).SubItems(13).Text & """" & vbCrLf
                    End If
                End If
            Next
            Dim filename = Format(Now(), "yyyy-MM-dd") & ".csv"
            My.Computer.FileSystem.WriteAllText(Application.StartupPath & "\" & filename, str, True, Encoding.Default)
            shuchu.Text &= "导出文件成功：" & Application.StartupPath & "\" & filename & vbCrLf
            MsgBox("导出成功，请在程序根目录下查看文件！", MsgBoxStyle.OkOnly, "提示")
        Catch ex As Exception
            MsgBox("导出出错！", MsgBoxStyle.OkOnly, "提示")
        End Try
    End Sub

    Private Sub Form1_Closed(sender As Object, e As EventArgs) Handles Me.Closed
        End
    End Sub
    Private Sub ListView1_DoubleClick(sender As Object, e As EventArgs) Handles ListView1.DoubleClick
        Try
            Dim url = ListView1.SelectedItems(0).SubItems(3).Text
            If IsNotWhiteSpace(url) Then
                System.Diagnostics.Process.Start(url)
            End If
        Catch ex As Exception

        End Try

    End Sub

    Private Sub Button6_Click(sender As Object, e As EventArgs) Handles Button6.Click

        'Dim html = CefWeb2.GetDomHtml()
        'Dim stars = getstars(html.ToLower)
        'Console.WriteLine(html)
        'Console.WriteLine(stars)
        'Dim formdata = "{""keyWords"":[""The North FaceFaceaaaa""],""sources"":[],""ncl"":[],""Keyword"":""The North Faceaaaa"",""status"":""0""}"
        'Dim html = getnewhtml("https://www.ip51.com/api/brand/search", "POST", formdata, "", "", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.164 Safari/537.36", "https://www.ip51.com/brand/search", "application/json")
        'Console.WriteLine(html)
        Dim html = waitfor(CefWeb, "https://www.miwaimao.com/test.php", "meow", "validatecaptcha", Me, "error/logo._ttd_.png", "ref=cs_404_logo")
        Dim brand = getbrand(html)
        Dim nodebrowser = getnodebrowser(html)
        Dim nodenodename = getnodenodename(html)
        Dim bsr = getbsr(html)
        Dim totalmarkurl = gettotalmarkurl(html)
        Dim fba = getfba(html)
        Dim price = getprice(html)
        Dim currency = getcurrency(price)
        Dim shipfee = getshipfee(html)
        Dim asin = getasin(html)
        Dim review = getreview(html)
        Dim stars = getstars(html)


        'Console.WriteLine(html)
        'Console.WriteLine("brand:" & brand)
        'Console.WriteLine("nodebrowser:" & nodebrowser)
        'Console.WriteLine("nodenodename:" & nodenodename)
        'Console.WriteLine("bsr:" & bsr)
        'Console.WriteLine("totalmarkurl:" & totalmarkurl)
        'Console.WriteLine("fba:" & fba)
        'Console.WriteLine("price:" & price)
        'Console.WriteLine("currency:" & currency)
        'Console.WriteLine("shipfee:" & shipfee)
        'Console.WriteLine("asin:" & asin)
        'Console.WriteLine("review:" & review)
        'Console.WriteLine("stars:" & stars)
    End Sub

    Private Sub Button7_Click(sender As Object, e As EventArgs) Handles Button7.Click
        'Console.WriteLine(CefWeb2.GetDomHtml())
        CefWeb.Load(keyword.Text)

        'CefWeb2.Load(keyword.Text)
        'Form5.Show()
        'CefWeb2 = CefBrowser.LoadBrowser(Form5.Pnl_Web, keyword.Text)
        'MsgBox(Form5.GetHashCode)
        'CefWeb2.Load(keyword.Text)
    End Sub

    Private Sub Button8_Click(sender As Object, e As EventArgs) Handles Button8.Click
        Dim sqlstr = "1=1"
        sqlstr &= " and Review>=" & pla.Value & " and Review<=" & plb.Value & " and Stars>=" & xja.Value & " and Stars<=" & xjb.Value
        If keyw.Text <> "" Then
            sqlstr &= " And Title Like '%" & trimsql(keyw.Text) & "%'"
        End If
        If js.Text <> "" Then
            sqlstr &= " and Status='" & js.Text & "'"
        End If
        If sb.Text <> "" Then
            sqlstr &= " and IsReg='" & sb.Text & "'"
        End If
        If is_china.Text <> "" Then
            sqlstr &= " and IsChina='" & is_china.Text & "'"
        End If
        If cy.Text <> "" Then
            sqlstr &= " and Country='" & cy.Text & "'"
        End If
        If taskname.Text <> "" Then
            sqlstr &= " and Taskname='" & taskname.Text & "'"
        End If
        Loadlist(sqlstr)
    End Sub

    Private Sub ListView2_SelectedIndexChanged(sender As Object, e As EventArgs) Handles ListView2.SelectedIndexChanged

    End Sub

    Private Sub ListView2_DoubleClick(sender As Object, e As EventArgs) Handles ListView2.DoubleClick
        Try
            Dim url = ListView2.SelectedItems(0).SubItems(2).Text
            'Console.WriteLine(url)
            If IsNotWhiteSpace(url) Then
                System.Diagnostics.Process.Start(url)
            End If
        Catch ex As Exception

        End Try
    End Sub

    Private Sub Button9_Click(sender As Object, e As EventArgs) Handles Button9.Click
        'Capture.ShowDialog()
        'Frm_Capture.ShowDialog(Me, "https://translate.google.cn/?sl=auto&tl=zh-CN&text=visita%20la%20de&op=translate")
        CefWeb.Load("https://www.miwaimao.com/test.php")
        'uploadxc = New System.Threading.Thread(New System.Threading.ParameterizedThreadStart(AddressOf caiji2))
        'uploadxc.Name = NameOf(caiji2)
        'uploadxc.Start()   '启动线程
        'Dim urlc = "https://www.miwaimao.com/test.php"
        'Dim html = waitfor(CefWeb, urlc, "meow", "validatecaptcha", Me, "error/logo._ttd_.png", "ref=cs_404_logo")


    End Sub

    Private Sub usecookie_Click(sender As Object, e As EventArgs) Handles usecookie.Click
        If listurl.Text = "" Then
            MsgBox("请先输入列表页网址", MsgBoxStyle.OkOnly, "提示")
            usecookie.Checked = False
            Exit Sub
        End If
        If usecookie.Checked Then
            Form4.Show()
            'Form4.CefWeb2.Load(listurl.Text)
        End If
    End Sub

    Private Sub btnbegin_Click(sender As Object, e As EventArgs) Handles btnbegin.Click
        Try
            If (listurl.Text = "" Or InStr(listurl.Text.ToLower, "www.amazon") <= 0) And iscontinue.Checked = False Then
                btnbegin.Enabled = True
                btnend.Enabled = False
                MsgBox("请输入正确的列表页网址！", MsgBoxStyle.OkOnly, "提示")
                Exit Sub
            End If
            If usecookie.Checked And listurl.Text = "" Then
                btnbegin.Enabled = True
                btnend.Enabled = False
                MsgBox("COOKIE不能为空！", MsgBoxStyle.OkOnly, "提示")
                Exit Sub
            End If
            allasin = ""
            isworking = True
            btnbegin.Enabled = False
            Dim sqlstr2 = "select * from t_TaskList order by ObjectDBID desc;"
            Dim reader2 As Data.Common.DbDataReader = DB(sqlstr2)
            If reader2.HasRows Then
                With reader2.Read
                    allasin &= CStr(reader2.Item("Dpurl"))
                End With
            End If
            If iscontinue.Checked Then
                Dim sqlstr = "select * from t_TaskList order by ObjectDBID desc limit 1;"
                Dim reader As Data.Common.DbDataReader = DB(sqlstr)
                If reader.HasRows Then
                    With reader.Read
                        starturl = CStr(reader.Item("Caurl"))
                    End With
                    If starturl = "" Then
                        starturl = listurl.Text
                    End If
                Else
                    MsgBox("记录为空！")
                End If
            Else
                starturl = listurl.Text
            End If
            Thecountry = getcounty(starturl)
            shuchu.Text = ""
            ListView1.Items.Clear()
            uploadxc = New System.Threading.Thread(New System.Threading.ParameterizedThreadStart(AddressOf newcaiji))
            uploadxc.Start()   '启动线程
            btnend.Enabled = True

        Catch ex As Exception
            btnend.Enabled = False
            btnbegin.Enabled = True
            isworking = False
            'MsgBox("已经停止", MsgBoxStyle.OkOnly, "提示")
            shuchu.Text &= "已经停止" & vbCrLf
        End Try
    End Sub
    Function getcounty(url)
        Try
            Dim ss = Split(url, "/")
            Dim cc = Replace(ss(2), "www.", "")
            Return cc
        Catch ex As Exception
            Return ""
        End Try
    End Function
    Sub newcaiji()
        Try
            Dim i = 0
            Dim mydomain = getdomain(starturl)
            Do
                If isworking = False Then
                    Invoke(Sub()
                               btnend.Enabled = False
                               btnbegin.Enabled = True
                               shuchu.Text &= "已经停止" & vbCrLf
                               Loadlist("")
                               MsgBox("已经停止", MsgBoxStyle.OkOnly, "提示")
                           End Sub)
                    Exit Sub
                End If

                CefWeb2.Load(starturl)
                Application.DoEvents()
                Thread.Sleep(2000)
                Dim pagehtml = waitfor(CefWeb2, starturl, "meow", "validatecaptcha", Me, "error/logo._ttd_.png", "ref=cs_404_logo")
                Dim page = "0"
                If InStr(starturl, "ref=sr_pg_") > 0 Then
                    Dim np = Split(starturl, "ref=sr_pg_")
                    page = np(UBound(np))
                End If
                Invoke(Sub()
                           shuchu.Text &= "开始抓取：" & starturl & vbCrLf
                       End Sub)


                If InStr(pagehtml, "<a class=""a-link-normal s-no-outline"" href=""") > 0 Then
                    Dim detailurls = Split(pagehtml, "<a class=""a-link-normal s-no-outline"" href=""")
                    'Console.WriteLine(detailurls.Count)
                    'Exit Sub
                    For r = 1 To UBound(detailurls)
                        Dim brand As String = ""
                        Dim isreg As String = "未知"
                        Dim asin As String = ""
                        Dim review As String = 0
                        Dim stars As String = 0
                        Dim status As String = "未知"
                        Dim nodebrowser As String = ""
                        Dim nodename As String = ""
                        'Dim price As String = 0
                        'Dim shipfee As String = 0
                        'Dim totalprice As String = 0
                        'Dim currency As String = "未知"
                        Dim bsr As String = 0
                        Dim ischina As String = "未知"
                        Dim totalmark As String = 0
                        Dim title As String = ""
                        Dim c_time As String = "未知"
                        Dim detailurl = mydomain & Replace(Split(detailurls(r), """>")(0), "amp;", "") & "&th=1&psc=1"
                        'Console.WriteLine(detailurl)
                        If InStr(allasin, detailurl) <= 0 Then
                            CefWeb2.Load(detailurl)
                            Application.DoEvents()
                            Thread.Sleep(2000)
                            Dim html = waitfor(CefWeb2, detailurl, "meow", "validatecaptcha", Me, "error/logo._ttd_.png", "ref=cs_404_logo")
                            If InStr(html, "add-to-cart-button") <= 0 And InStr(html, "cs_404_logo") <= 0 Then
                                status = "是"
                            Else
                                status = "否"
                            End If
                            brand = getbrand(html)
                            review = GetreviewNumbers(getreview(html))
                            stars = getstars(html)
                            asin = getasin(html)
                            title = gettitle(html)
                            bsr = getbsr(html)
                            nodebrowser = getnodebrowser(html)
                            nodename = getnodenodename(html)
                            'price = getprice(html)
                            c_time = getc_time(html)
                            'shipfee = getshipfee(html)
                            ischina = getfba(html)
                            'currency = getcurrency(price)
                            'price = GetNumbers(price)
                            'totalprice = CDec(price) + CDec(shipfee)
                            If title <> "" Then
                                'Dim totalmarkurl = mydomain & gettotalmarkurl(html)
                                ''Console.WriteLine("totalmarkurl:" & totalmarkurl)
                                'If totalmarkurl <> mydomain Then
                                '    CefWeb.Load(totalmarkurl)
                                '    Application.DoEvents()
                                '    Thread.Sleep(2000)
                                '    Dim totalmarkhtml = waitfor(CefWeb, totalmarkurl, "meow", "validatecaptcha", Me, "error/logo._ttd_.png", "ref=cs_404_logo")
                                '    totalmark = GetreviewNumbers(gettotalmark(totalmarkhtml))
                                'End If
                                'If totalmark = 0 And ischina = "是" Then
                                '    Console.WriteLine("totalmarkurl:" & totalmarkurl)
                                'End If
                                If tradecookie <> "" And brand <> "" And brand.Length < 100 Then
                                    If brandlistname = "tmdn.org" Then
                                        Dim htmls = getnewhtml("https://www.tmdn.org/tmview/api/search/autocomplete/basicSearch?text=" & HttpUtility.UrlEncode(brand), "GET", "", tradecookie)
                                        If InStr(htmls, "here are your search results") > 0 Then
                                            isreg = "已注册"
                                        Else
                                            isreg = "未注册"
                                        End If
                                    End If
                                    If brandlistname = "ip51.com" Then
                                        Dim formdata = "{""keyWords"":[""" & brand & """],""sources"":[],""ncl"":[],""Keyword"":""" & brand & """,""status"":""0""}"
                                        Dim htmls = getnewhtml("https://www.ip51.com/api/brand/search", "POST", formdata, "", "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/91.0.4472.164 Safari/537.36", "https://www.ip51.com/brand/search", "*/*", "application/json")
                                        If InStr(htmls, "{""brands"":[]}") > 0 Then
                                            isreg = "未注册"
                                        Else
                                            isreg = "已注册"
                                        End If
                                    End If
                                    If brandlistname = "trademarkia.com" Then
                                        Dim nurl = "https://www.trademarkia.com/trademarks-search.aspx?tn=" & HttpUtility.UrlEncode(brand)
                                        CefWeb2.Load(nurl)
                                        'Thread.Sleep(1000)
                                        Console.WriteLine(nurl)
                                        Dim htmls = waitfor(CefWeb2, nurl, "txt txt-lg")
                                        If InStr(htmls.ToLower, "here are your search results") > 0 Then
                                            isreg = "已注册"
                                        Else
                                            isreg = "未注册"
                                        End If
                                    End If
                                End If
                                i += 1
                                allasin &= detailurl
                                Dim t As New TaskList
                                t.Asin = asin
                                t.Brand = brand
                                t.BSR = bsr
                                t.Caurl = starturl
                                't.Currency = currency
                                t.Nodebrowser = nodebrowser
                                t.Nodename = nodename
                                t.C_Time = c_time
                                t.IsReg = isreg
                                t.Dpurl = detailurl
                                t.IsChina = ischina
                                t.Page = page
                                't.Price = price
                                t.Review = review
                                t.Stars = stars
                                t.Status = status
                                t.Totalmark = totalmark
                                't.Totalprice = totalprice
                                t.Title = title
                                't.Shipfee = shipfee
                                t.Country = Thecountry
                                t.Add()
                                Invoke(Sub()
                                           Dim itm As ListViewItem
                                           itm = ListView3.Items.Add(ListView3.Items.Count + 1)
                                           itm.SubItems.AddRange({"", CStr(asin), CStr(trimsql(detailurl)), CStr(title), CStr(nodebrowser), CStr(nodename), CStr(review), CStr(stars), CStr(brand), CStr(isreg), CStr(status), CStr(ischina), CStr(bsr), CStr(c_time), CStr(Thecountry)})
                                           shuchu.Text = "当前已获取第：" & page & "页，获得产品数：" & i & "个"
                                       End Sub)
                            End If
                        End If
                    Next
                End If
                Dim splitstra = """ class=""s-pagination-item s-pagination-next"
                Dim splitstrb = "a-last""><a href="""
                If InStr(pagehtml, splitstra) > 0 Or InStr(pagehtml, splitstrb) > 0 Then
                    If InStr(pagehtml, splitstra) > 0 Then
                        Dim nextpagea = Split(pagehtml, splitstra)(0)
                        Dim nextpageb = Split(nextpagea, "<a href=""")
                        Dim nextpage = Replace(nextpageb(UBound(nextpageb)), "amp;", "")
                        starturl = mydomain & nextpage
                    End If
                    If InStr(pagehtml, splitstrb) > 0 Then
                        Dim nextpagea = Split(pagehtml, splitstrb)(1)
                        Dim nextpageb = Split(nextpagea, """>")(0)
                        Dim nextpage = Replace(nextpageb, "amp;", "")
                        starturl = mydomain & nextpage
                    End If
                    'Console.WriteLine(page & ":" & starturl)
                ElseIf InStr(pagehtml, "s-pagination-strip") > 0 Then
                    Invoke(Sub()
                               Btend.Enabled = False
                               Btstart.Enabled = True
                               shuchu.Text &= "已经停止" & vbCrLf
                               isworking = False
                           End Sub)
                    Exit Sub
                End If
                'Exit Sub
                'Console.WriteLine("aaaaa")
                Thread.Sleep(2000)
            Loop

        Catch ex As Exception
            'Console.WriteLine(ex)
            Invoke(Sub()
                       Btend.Enabled = False
                       Btstart.Enabled = True
                       shuchu.Text &= "已经停止" & vbCrLf
                       isworking = False
                       Loadlist("")
                       'MsgBox("已经停止", MsgBoxStyle.OkOnly, "提示")
                   End Sub)
        End Try
    End Sub

    Private Sub btnend_Click(sender As Object, e As EventArgs) Handles btnend.Click
        Try
            isworking = False
            If uploadxc.IsAlive Then
                uploadxc.Abort()
            End If
            GC.Collect()
            GC.WaitForFullGCApproach()
            btnend.Enabled = False
            btnbegin.Enabled = True
            shuchu.Text &= "已经停止" & vbCrLf
            MsgBox("已经停止", MsgBoxStyle.OkOnly, "提示")
        Catch ex As Exception
            btnend.Enabled = False
            btnbegin.Enabled = True
            isworking = False
            shuchu.Text &= "已经停止" & vbCrLf
            'MsgBox("已经停止", MsgBoxStyle.OkOnly, "提示")
        End Try
    End Sub

    Private Sub ListView3_DoubleClick(sender As Object, e As EventArgs) Handles ListView3.DoubleClick
        Try
            Dim url = ListView3.SelectedItems(0).SubItems(3).Text
            If IsNotWhiteSpace(url) Then
                System.Diagnostics.Process.Start(url)
            End If
        Catch ex As Exception

        End Try
    End Sub
    Private Sub Button10_Click(sender As Object, e As EventArgs) Handles Button10.Click
        If keyword.Text = "" Then
            MsgBox("请先输入关键词", MsgBoxStyle.OkOnly, "提示")
        Else
            keywords.CheckedListBox1.Items.Add(keyword.Text)
            stcountry = country.Text
            keywords.Show()
        End If
    End Sub

    Private Sub country_SelectedValueChanged(sender As Object, e As EventArgs) Handles country.SelectedValueChanged
        loadkwy()
    End Sub
End Class